//
//  FirstCollectionViewCell.m
//  MarianasTrench
//
//  Created by wangjue on 16/3/17.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "FirstCollectionViewCell.h"
@interface FirstCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataSourceArr;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, retain) UILabel *label;


@end

@implementation FirstCollectionViewCell



-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.num = 0;
        [self createTableView];
        
    }
    return self;
}

///查询历史的信息
-(void)selectMArr{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"History" inManagedObjectContext:[CoreDataManager shareManager].managedObjectContext];
    //设置搜索的实体
    [fetchRequest setEntity:entity];
    
    NSError *error = nil;
    //搜索返回数据
    NSArray *fetchedObjects = [[CoreDataManager shareManager].managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"error : %@", error);
    }
    
    //对查询后的数组倒叙输出
    self.dataSourceArr = [[fetchedObjects reverseObjectEnumerator] allObjects];
}

-(void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 23, self.contentView.frame.size.width, self.contentView.frame.size.height - 64) style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.contentView addSubview:_tableView];
    
    //注册关注的tableViewcell
    [_tableView registerClass:[FirstTableViewCell class] forCellReuseIdentifier:@"FirstTableViewCell"];
    
    //注册收藏的tableViewcell
    [_tableView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"SecondTableViewCell"];
    
    //注册历史的tableViewcell
    [_tableView registerClass:[ThirdTableViewCell class] forCellReuseIdentifier:@"ThirdTableViewCell"];
    
    
}




#pragma mark - tableView协议方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_currentNum == 0) {
        FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FirstTableViewCell"];
        ListenModel *model = _firstModelArr[indexPath.row];
        cell.model = model;
        cell.backgroundColor = [UIColor clearColor];
        return cell;

    }
    
    if (_currentNum == 1) {
        SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecondTableViewCell"];
        ListenModel *model = _secModelArr[indexPath.row];
        cell.model = model;
        return cell;
    }

    
    if (_currentNum == 2) {
        ThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThirdTableViewCell"];
        
        History *history = _dataSourceArr[indexPath.row];
        cell.history = history;
        
        return cell;
    }
    
    return nil;
    
  }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_currentNum == 0) {
        [self selectMArr];
        
        if (NULL != _label) {
        [_label removeFromSuperview];
        }
        
        return _firstModelArr.count;
    }
    
    
    
    
    if (_currentNum == 1) {
        [self selectMArr];
        
        if (NULL != _label) {
        [_label removeFromSuperview];
        }
        
        return _secModelArr.count;
    }
    
    
    
    if (_currentNum == 2) {
        [self selectMArr];
        
        
        if (NULL != _label) {
            [_label removeFromSuperview];
        }
        
        if (_dataSourceArr.count == 0) {
            self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.contentView.frame.size.height / 2, self.contentView.frame.size.width, 20)];
            _label.text = @"您当前还没有任何浏览历史记录";
            _label.textAlignment = NSTextAlignmentCenter;
            [self.contentView addSubview:_label];
        }

        return _dataSourceArr.count;
    }
    
    return 0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (_currentNum == 0) {
        return 160;
    }
    
    if (_currentNum == 1) {
        return 130;
    }
    
    return 130;

  }



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_currentNum == 0) {
        
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"History" inManagedObjectContext:[CoreDataManager shareManager].managedObjectContext];
        //设置搜索的实体
        [fetchRequest setEntity:entity];
 
        //以某个字段排序 第二个参数 是否升旭
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"imgStr"
                                                                       ascending:YES];
        [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
        
        NSError *error = nil;
        //搜索返回数据
        NSArray *fetchedObjects = [[CoreDataManager shareManager].managedObjectContext executeFetchRequest:fetchRequest error:&error];
        if (fetchedObjects == nil) {
            NSLog(@"error : %@", error);
        }
         NSLog(@"路径:%@", [[CoreDataManager shareManager]applicationDocumentsDirectory]);
        
        self.dataSourceArr = [NSMutableArray arrayWithArray:fetchedObjects];
        
        ListenModel *model = _firstModelArr[indexPath.row];
    
        for (History *history in fetchedObjects) {
            if ([history.imgStr isEqualToString:model.albumCover]) {
                _num++;
                break;
            }
        }
        
        if (_num == 0) {
            //前两行缩写，另一种创建方式
            History *history = [NSEntityDescription insertNewObjectForEntityForName:@"History" inManagedObjectContext:[CoreDataManager shareManager].managedObjectContext];
            
            history.imgStr = model.albumCover;
            history.textLabel = model.albumTitle;
            history.textDownLabel = model.trackTitle;
            history.mp3Url = model.playPath32;
            
            
            //保存
            [[CoreDataManager shareManager]saveContext];
         
        }
        
        
        MusicPlayerViewController *VC = [[MusicPlayerViewController alloc] init];
        VC.playMusicArr = [NSMutableArray arrayWithArray:_firstModelArr];
        
        VC.indexNum = indexPath.row;
        [self.delegate skipNewVC:VC];
        
    }
    
    
    if (_currentNum == 1) {
        WJCollectionViewController *VC = [[WJCollectionViewController alloc] init];
        ListenModel *model = _secModelArr[indexPath.row];
        VC.str = model.albumId;
        [self.delegate skipVC:VC];
        
    
    }
    
    if (_currentNum == 2) {
        
        MusicPlayerViewController *VC = [[MusicPlayerViewController alloc] init];
        VC.indexNum = indexPath.row;
        NSMutableArray *listenArr = [NSMutableArray array];
        for (History *history in _dataSourceArr) {
            ListenModel *model = [[ListenModel alloc] init];
            model.coverLarge = history.imgStr;
            model.playUrl32 = history.mp3Url;
            model.coverMiddle = history.imgStr;
            model.title = history.textLabel;
            [listenArr addObject:model];
        }
        VC.playMusicArr = [NSMutableArray arrayWithArray:listenArr];
        [self.delegate skipNewVC:VC];
       
        
    }
}


- (void)setCurrentNum:(NSInteger)currentNum {
    _currentNum = currentNum;
    
    [_tableView reloadData];
}




@end
